package com.example.bookmanagementsystem.interceptor;

import com.example.bookmanagementsystem.constant.Constants;
import com.example.bookmanagementsystem.entity.UserInfo;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import java.util.Arrays;
import java.util.List;

@Component
@Slf4j
public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        log.info("登录拦截器校验......");

        HttpSession httpSession = request.getSession();
        UserInfo userInfo = (UserInfo) httpSession.getAttribute(Constants.SESSION_USER_KEY);
        if (userInfo != null && userInfo.getId() > 0) {
            return true;
        }
        response.setStatus(401);//401 表示未认证登录
        return false;
    }

    //@Override
    //public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
    //    log.info("目标方法执行后");
    //}

    //@Override
    //public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
    //    log.info("视图渲染完毕后执行，最后执行");
    //}
}
